package com.iwdnb.gkgz.ui.controller;

import com.iwdnb.bmnf.common.model.result.Result;
import com.iwdnb.gkgz.application.service.source.NetsStockDataSource;
import com.iwdnb.gkgz.application.vo.RealtimeStockData;
import com.iwdnb.gkgz.application.vo.StockType;
import com.iwdnb.gkgz.infrastructure.dao.entity.StockDO;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 股票实时数据处理
 *
 * @author pengl
 * @date 2020/11/09
 **/
@RestController
@RequestMapping("/api//stock/realtime")
public class StockRealtimeDataController {

    @Autowired
    NetsStockDataSource netsStockDataSource;
    @Autowired
    private StockRepository stockRepository;

    /**
     * 获取股票的最后数据信息，价格，涨幅，成交数据，优先从缓存中读取，若无则去第三方数据源读取
     *
     * @param code
     * @return
     */
    @RequestMapping(value = "/data", method = RequestMethod.GET)
    public Result<RealtimeStockData> getRealtimeData(@RequestParam String code) {
        StockDO stockDO = stockRepository.findByCode(code);
        if (stockDO == null) {
            return Result.getErrorResult("股票代码不存在，请联系管理员");
        }
        RealtimeStockData realtimeData = netsStockDataSource.getRealtimeData(code,
            StockType.getValueByType(stockDO.getMarket()));

        return Result.getSuccessResult(realtimeData);
    }

}
